from  flask import Flask

from app.routers import mountedRouter
from app.db import db
from  flask_login import LoginManager
from app.response import ResponseBuilder

def unauthorized():
    return ResponseBuilder().error(401, 'Unauthorized')

def create_app():
    app = Flask(__name__)
    app.config.from_object('config.DevelopmentConfig')

    loginManager = LoginManager()
    loginManager.init_app(app)
    loginManager.unauthorized_handler(unauthorized)

    @loginManager.user_loader
    def load_user(user_id):
        from app.models.UserModel import User
        return User.query.get(int(user_id))
    

    db.init_app(app)
    with app.app_context():
        db.create_all()

    mountedRouter(app)

    return app

